package com.huike.report.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.SysUserMssage;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysUserMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.core.domain.entity.SysUser;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.domain.vo.InfoVo;
import com.huike.report.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysDeptMapper sysDeptMapper;

    @Autowired
    TbAssignRecordMapper tbAssignRecordMapper;
    @Autowired
    TbContractMapper contractMapper;

    @Autowired
    TbBusinessMapper tbBusinessMapper;

    @Autowired
    TbClueMapper tbClueMapper;

    @Autowired
    ReportServiceImpl reportService;

    /*线索转龙虎榜*/
    @Override
    public List<Map<String, Object>> salesStatistic(IndexStatisticsVo indexStatisticsVo) {
        //统计所有的线索
        String beginCreateTime = indexStatisticsVo.getBeginCreateTime();
        String endCreateTime = indexStatisticsVo.getEndCreateTime();
        InfoVo infoVo = reportService.getVulnerabilityMap(beginCreateTime, endCreateTime);
        Integer all = infoVo.getCluesNums();

        List<Map<String, Object>> list1 = tbClueMapper.countAllClueByUser(indexStatisticsVo);
        for (Map<String, Object> stringObjectMap : list1) {
            Long num = (Long) stringObjectMap.get("num");
            BigDecimal radio = getRadio(all, num);
            stringObjectMap.put("radio", radio);
        }
        return list1;
    }


    /**
     * 转换率
     * @param all
     * @param num
     * @return
     */
    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }
}